package com.bwda.dsrs.filecenter.config;

import com.bwda.dsrs.base.util.ThreeDesUtil;
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 初始化数据解密
 *
 * @author yanyuemei
 * @date
 */
@Configuration
public class EncryptionPropertyConfig {

    private static final String DES_HEADER_STR="DES@";

    @Bean(name = "encryptablePropertyResolver")
    public EncryptablePropertyResolver encryptablePropertyResolver() {
        return new EncryptionPropertyResolver();
    }

    class EncryptionPropertyResolver implements EncryptablePropertyResolver {

        @Override
        public String resolvePropertyValue(String value) {
            if (StringUtils.isBlank(value)) {
                return value;
            }
            // 值以DES@开头的均为DES加密,需要解密
            if (value.startsWith(DES_HEADER_STR)) {
                return resolveDesValue(value.substring(4));
            }
            // 不需要解密的值直接返回
            return value;
        }

        private String resolveDesValue(String value) {
            // 自定义DES密文解密
            return ThreeDesUtil.decode(value);
        }

    }
}
